// pages/Xunjiajilu/Xunjiajilu.js
var tool = require("../Tongyong/tongyong.js");//调取共用类的方法
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    min: 0,//最少字数  
    max: 1000, //最多字数 (根据自己需求改变) 
    tempFilePaths: [],//存放选择后的图片
    neirong:'',
    dataarray:[],//全部记录
    xiaoxi:'',
    ishuifushuru: false,
    ishuifubut:false,
    inquiry_id:'',
    userInfo:{},
  },

  //字数限制    
  inputs: function (e) {
    // 获取输入框的内容  
    var value = e.detail.value;
   
    // 获取输入框内容的长度  
    var len = parseInt(value.length);

    //最多字数限制  
    if (len > this.data.max) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行  
    this.setData({
      currentWordNumber: len, //当前字数 
      neirong: value,   
    });
  },  
 

//点击上传
  shangchuanimageBut: function (e){
    var that = this;
    var imagelength = that.data.tempFilePaths.length;
    wx.chooseImage({
      count: 9 - imagelength, // 默认9  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        console.log(res);
        var array = that.data.tempFilePaths;
        array=array.concat(res.tempFilePaths);

        console.log(array);

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        that.setData({
          tempFilePaths: array
        })
      }
    })
  },

  //删除图片方法
  shangchuBut:function(e){
    var that = this;
    const index = e.currentTarget.dataset.index; //得到index的值
    console.log(index);
    var notes = that.data.tempFilePaths;
    notes.splice(index,1);//根据索引删除指定元素个数
    
    that.setData({
      tempFilePaths: notes
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    self.getdatarequest();
  },

  bindTextAreaBlur: function (e) {
    console.log(e.detail.value)
  },
  
  //提交的时候获取formid
  submitInfo: function (e) {
    console.log('GG 敌方军团已同意投降 4票赞成 0票反对')
    console.log(e.detail.formId);  
  },

  //提交方法
  tijiaotupian:function(e){

    var self=this;
    
    if (self.data.neirong.length <= 0 && self.data.tempFilePaths.length <=0){

      wx.showModal({
        title: '提示',
        content: '请完善提交内容',
        showCancel:false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }

        }
      })

      return;
    }

    self.setData({
      loadingHidden: false,
    });

    if (self.data.tempFilePaths.length>0){
      
      tool.uploadimg({
        url: 'https://xcx.xiuma.cn/home/Index/upImages',//这里是你图片上传的接口
        path: self.data.tempFilePaths,//这里是选取的图片的地址数组
        pic: [],
      }, function (res) {
        var pictureurl = res;
        var VID = '';
        wx.getStorage({
          key: 'viewitorid',
          success: function (res) {
            VID = res.data;
            var qingqiudata = {
              pictureurl: pictureurl,
              content: self.data.neirong,
              visitor_id: VID
            };
            // var b = tool.getString(a);
            tool.Httprequest('saveInquiry', qingqiudata, function (res) {
              console.log(res);
              self.getdatarequest();
            }, function (res) {

            }, function (res) {
              self.setData({
                currentWordNumber: 0, //当前字数 
                neirong: '',
                tempFilePaths: [],
                loadingHidden: true,
              });
              wx.showToast({
                title: '询价信息提交成功',
                icon: 'succes',
                duration: 1000,
                mask: true
              });
            })

          }
        })
        console.log(res);
      });

    }else{
      
      var pictureurl = '';
      var VID = '';
      wx.getStorage({
        key: 'viewitorid',
        success: function (res) {
          VID = res.data;
          var qingqiudata = {
            pictureurl: pictureurl,
            content: self.data.neirong,
            visitor_id: VID
          };
          // var b = tool.getString(a);
          tool.Httprequest('saveInquiry', qingqiudata, function (res) {
            console.log(res);

            self.getdatarequest();

          }, function (res) {

          }, function (res) {
            self.setData({
              currentWordNumber: 0, //当前字数 
              neirong: '',
              tempFilePaths: [],
              loadingHidden: true,
            });
            wx.showToast({
              title: '询价信息提交成功',
              icon: 'succes',
              duration: 1000,
              mask: true
            });

          });

        }
      })
    }

  },


  /***更新数据方法 */
  getdatarequest: function () {
    var self = this;

    self.setData({
      loadingHidden: false,
    });

    var VID = '';
    wx.getStorage({
      key: 'viewitorid',
      success: function (res) {
        VID = res.data;
        var qingqiudata = {
          visitor_id: VID
        };
        // var b = tool.getString(a);
        tool.Httprequest('getInquiryInfo', qingqiudata, function (res) {
          self.setData({
            dataarray: res
          });
          console.log(self.data.dataarray);
        }, function (res) {

        }, function (res) {
          self.setData({
            loadingHidden: true,
          });
          wx.stopPullDownRefresh();
        });

      }
    })

  },

  /**   
     * 预览图片  
     */
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var index = e.target.dataset.ind;//当前段落

    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.dataarray[index].pictureurl // 需要预览的图片http链接列表  
    })
  },

  //点回复的时候出发
  getUserInfo: function (e) {
    
   //判断是否拒绝
    if (e.detail.userInfo == undefined){
      return;
    }
    var self = this;
       
    if (self.data.ishuifubut == true) {
      self.setData({
        ishuifubut:false,
        ishuifushuru: false,
      })
    } else {
      self.setData({
        ishuifubut:true,
        ishuifushuru: true,
      })

    }
    //存储当前的信息id
    self.setData({
      inquiry_id: e.target.dataset.inquiry_id,
      userInfo: e.detail.userInfo,
    })


  },
  
  /***防止冒泡内容不实现 */
  kongfangfa: function (e) {

  },

  //消息编辑框失去焦点的时候
  xiaoxishiqujiaodian:function(e){
    var self = this;
    self.setData({
      ishuifushuru: false,
    })
  },

  //发送回复
  fasonghuifuAction:function(e){
    var self = this;
    self.setData({
      ishuifubut: false,
      ishuifushuru: false,
    })
    console.log(this.data.xiaoxi);

    var VID = '';
    wx.getStorage({
      key: 'viewitorid',
      success: function (res) {
        VID = res.data;
        var qingqiudata = {
          visitor_id: VID,
          inquiry_id: self.data.inquiry_id,
          comment: self.data.xiaoxi,
          avatarUrl: self.data.userInfo.avatarUrl,
          nickName: self.data.userInfo.nickName,
        };
        
        self.setData({
          loadingHidden: false,
        });

        // var b = tool.getString(a);
        tool.Httprequest('setComment', qingqiudata, function (res) {
          console.log(res);
          self.getdatarequest();
        }, function (res) {

        }, function (res) {
          self.setData({
            loadingHidden: true,
          });
          wx.showToast({
            title: '回复成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          });

        });
      }
    })

  },

  //消息编写框改变的方法
  xiaoxiinputs:function(e){
    var self=this;
    var value = e.detail.value;
    self.setData({
      xiaoxi: value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self = this;
    self.getdatarequest();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})